package com.bestcem.xm.om.service.email;

import com.bestcem.xm.om.config.properties.OmEmailProperties;
import com.bestcem.xm.om.constant.CommonConstant;
import com.bestcem.xm.om.dao.EmailBoxDao;
import com.bestcem.xm.om.entity.EmailBox;
import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * 享元模式创建邮件发送对象池
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/1 16:53
 */
@Component
public class JavaEmailSenderPoolImpl implements JavaEmailSenderPool {
    /**
     * 邮件发送服务类的map，key为邮箱号
     */
    private static final Map<String, JavaMailSenderImpl> SENDER_MAP = new HashMap<>();

    @Resource
    private EmailBoxDao emailBoxDao;
    @Resource
    private OmEmailProperties omEmailProperties;

    @Override
    public JavaMailSenderImpl getSender(String email) {
        JavaMailSenderImpl sender = SENDER_MAP.get(email);
        if (Objects.isNull(sender)) {
            // 此处没有高并发情况
            EmailBox emailBox = emailBoxDao.findByAccount(email);
            if (Objects.nonNull(emailBox)) {
                sender = generateSender(emailBox);
                SENDER_MAP.put(emailBox.getAccount(), sender);
            }
        }
        return sender;
    }

    @Override
    public JavaMailSenderImpl getDefaultSender() {
        String email = omEmailProperties.getDefaultEmail();
        if (StringUtils.isEmpty(email)) {
            return null;
        }
        JavaMailSenderImpl sender = this.getSender(email);
        if (Objects.isNull(sender)) {
            EmailBox emailBox = new EmailBox();
            emailBox.setAccount(email);
            emailBox.setPassword(omEmailProperties.getDefaultPwd());
            emailBox.setHost(omEmailProperties.getDefaultHost());
            emailBox.setPort(omEmailProperties.getDefaultPort());
            emailBox.setDescription("倍市得CEM");
            emailBox.setSsl(true);
            emailBox.setValidateStatus(true);
            emailBoxDao.save(emailBox);
            reloadSender(emailBox);
        }
        return this.getSender(email);
    }

    @Override
    public void reloadSender(EmailBox emailBox) {
        JavaMailSenderImpl sender = generateSender(emailBox);
        SENDER_MAP.put(emailBox.getAccount(), sender);
    }

    /**
     * 创建邮件发送服务类
     *
     * @param emailBox 邮箱对象
     * @return org.springframework.mail.javamail.JavaMailSenderImpl
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2021/12/23 17:46
     */
    private JavaMailSenderImpl generateSender(EmailBox emailBox) {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setProtocol(omEmailProperties.getSmtpProtocol());
        javaMailSender.setHost(emailBox.getHost());
        javaMailSender.setUsername(emailBox.getAccount());
        javaMailSender.setPassword(emailBox.getPassword());
        javaMailSender.setDefaultEncoding(omEmailProperties.getSmtpEncoding());
        Properties properties = new Properties();
        // 设置超时时间
        properties.setProperty(CommonConstant.MAIL_SMTP_TIMEOUT, omEmailProperties.getSmtpTimeout());

        // 如果是ssl的话,需要设置以下属性
        if (Boolean.TRUE.equals(emailBox.getSsl())) {
            properties.setProperty(CommonConstant.MAIL_SMTP_AUTH, omEmailProperties.getSmtpAuth());
            properties.setProperty(CommonConstant.MAIL_SMTP_SOCKETFACTORY_CLASS,
                    omEmailProperties.getSmtpSocketFactoryClass());
            properties.setProperty(CommonConstant.MAIL_SMTP_SOCKETFACTORY_PORT, String.valueOf(emailBox.getPort()));
        }
        javaMailSender.setJavaMailProperties(properties);
        return javaMailSender;
    }
}